package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
类简介：

功能：

时间：2020/4/11

*/
@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @Reference
    private UserService userService;

    //获取用户名用于回显
    @RequestMapping("getUsername")
    public Result getUsername() {

        try {
            //当Spring security完成认证后 ，会将用户信息保存到框架提供的上下文对象中去
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            User user = (User) authentication.getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }

    //分页查询
    @RequestMapping("/findPage")
    public Result findPages(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = userService.PageQuery(queryPageBean);
            return new Result(true, "查询会员信息成功", page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员信息失败", "网络繁忙,请稍后重试!!");
        }
    }


    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        try {
            userService.add(user, roleIds);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL, e.getMessage());
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            com.itheima.pojo.User user = userService.findById(id);
            return new Result(true, "获取用户信息成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户信息失败！");
        }
    }

    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try {
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true, "获取用户角色信息成功",roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户角色信息失败！","网络繁忙，请稍后再试！");
        }
    }

    @RequestMapping("/update.do")
    public Result update(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {
            userService.editUser(user,roleIds);
            return new Result(true, "修改成功",roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败！","网络繁忙，请稍后再试！");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败！","网络繁忙，请稍后再试！");
        }
    }
}
